package com.base.controller;

import com.base.po.voip.Bonus;
import com.base.service.RewardsService;
import com.base.service.SubscribersService;
import com.base.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分红统计
 * 
 * @author f
 *
 */
@Controller
@RequestMapping("/admin")
public class index_Bonus {
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private RewardsService rewardsService; // 任务信息
	@Autowired
	private UsersService usersService; // 任务信息

	/**
	 * 首页
	 *
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/bonusIndex")
	public ModelAndView bonusIndex(HttpServletRequest request) throws Exception {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
		Map<String, Object> map = new HashMap<>(); // 存储数据对象
		// 设置分页 获取内容
		List<Bonus> bonusList = new ArrayList<>();

		Bonus bonus = null;
		int bonusNum = 0;
		String levelName = "";
		for (int i = 0; i < 18; i++){
			bonus = new Bonus();
			if (i < 9){
				if (adminUsergrade == 1){
					bonusNum = subscribersService.selectByBonus(userid, null, i + 1, null, null);
				}else {
					bonusNum = subscribersService.selectByBonus(null, userid, i + 1, null, null);
				}
				levelName = "一店" + (i + 1) + "星";
			}else if (i >= 9 && i < 15){
				if (adminUsergrade == 1){
					bonusNum = subscribersService.selectByBonus(userid, null, null, i - 5, null);
				}else {
					bonusNum = subscribersService.selectByBonus(null, userid, null, i - 5, null);
				}
				levelName = "二店" + (i - 5) + "星";
			}else{
				if (adminUsergrade == 1){
					bonusNum = subscribersService.selectByBonus(userid, null, null, null, i - 8);
				}else {
					bonusNum = subscribersService.selectByBonus(null, userid, null, null, i - 8);
				}
				levelName = "三店" + (i - 8) + "星";
			}

			bonus.setLevel(i + 1);
			bonus.setLevelName(levelName);
			bonus.setBonusNum(bonusNum);
			bonusList.add(bonus);
		}

		map.put("bonusList", bonusList);

		return new ModelAndView("jsp/index_Bonus", map);
	}
}
